package com.devg.file.web.base.utils;

import com.github.pagehelper.Page;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageInfoUtils<T> implements Serializable {

  private static final long serialVersionUID = 1L;

  //当前页
  private int pageNum;
  //每页的数量
  private int pageSize;
  //总记录数
  private long total;
  //总页数
  private int pages;
  //结果集
  private List<T> list = new ArrayList<T>();
  //是否有下一页
  private boolean hasNextPage = false;

  private Map<String, Object> otherAttribute = new HashMap<>();

  public PageInfoUtils() {
  }

  /**
   * 包装Page对象
   *
   * @param list page结果
   */
  public PageInfoUtils(List<T> list) {
    if (list instanceof Page) {
      Page page = (Page) list;
      this.pageNum = page.getPageNum();
      this.pageSize = page.getPageSize();
      this.pages = page.getPages();
      this.list = page;
      this.total = page.getTotal();
      this.hasNextPage = this.pages > this.pageNum ? true : false;
    } else if (list instanceof Collection) {
      this.pageNum = 1;
      this.pageSize = list.size();
      this.pages = this.pageSize > 0 ? 1 : 0;
      this.list = list;
      this.total = list.size();
      this.hasNextPage = this.pages > this.pageNum ? true : false;
    }
  }

  /**
   * 包装Page对象
   *
   * @param list page结果
   */
  public PageInfoUtils(List<T> list, long total, Integer pageNum, Integer pageSize) {
    this.list = list;
    this.total = total;
    this.pageNum = pageNum;
    this.pageSize = pageSize;
    long totalPage = total % pageSize == 0 ? total / pageSize : (total / pageSize + 1);
    this.pages = (int) totalPage;
    this.hasNextPage = this.pages > this.pageNum ? true : false;
  }

  public PageInfoUtils(List<T> list, Integer pageNum, Integer pageSize) {
    if (null == list || list.size() <= 0) {
      this.total = 0;
      this.list = new ArrayList<>();
      this.pageNum = pageNum;
      this.pageSize = pageSize;
      this.pages = 0;
      this.hasNextPage = false;
    } else {
      int pages = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
      if (pageNum > pages) {
        pageNum = pages;
      }
      int start = (pageNum - 1) * pageSize;
      int end = pageNum * pageSize;
      if (end > list.size()) {
        end = list.size();
      }
      this.total = list.size();
      this.list = list.subList(start, end);
      this.pageNum = pageNum;
      this.pageSize = pageSize;
      this.pages = pages;
      this.hasNextPage = this.pages > this.pageNum ? true : false;
    }

  }

  public int getPageNum() {
    return pageNum;
  }

  public void setPageNum(int pageNum) {
    this.pageNum = pageNum;
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public long getTotal() {
    return total;
  }

  public void setTotal(long total) {
    this.total = total;
  }

  public int getPages() {
    return pages;
  }

  public void setPages(int pages) {
    this.pages = pages;
  }

  public List<T> getList() {
    return list;
  }

  public void setList(List<T> list) {
    this.list = list;
  }

  public boolean isHasNextPage() {
    return hasNextPage;
  }

  public void setHasNextPage(boolean hasNextPage) {
    this.hasNextPage = hasNextPage;
  }

  public Map<String, Object> getOtherAttribute() {
    return otherAttribute;
  }

  public void setOtherAttribute(Map<String, Object> otherAttribute) {
    this.otherAttribute = otherAttribute;
  }

  public void putOtherAttribute(String key, Object value) {
    this.otherAttribute.put(key, value);
  }
}
